//
//  DCFUIMediator.m
//  DCF
//
//  Created by 吴文拼 on 2017/12/26.
//  Copyright © 2017年 吴文拼. All rights reserved.
//

#import "DCFUIMediator.h"
#import "DCFLoginManager.h"
#import "DCFLoginHomeViewController.h"
#import "DCFNavigationController.h"
#import "DCFHomeWebUrlViewController.h"
#import "DCFInputPhoneViewController.h"
#import "DCFNetworkManager.h"
#import "DCFCheckingViewController.h"
#import "NSString+CommonUseTool.h"

@interface DCFUIMediator()

@property (nonatomic ,   copy)NSString *homeUrl;

@end

@implementation DCFUIMediator

+ (instancetype)sharedInstance
{
    static DCFUIMediator *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[DCFUIMediator alloc] init];
    });
    
    return instance;
}

-(void)createMainView{
    [DCFNetworkManager fetchDCFJSONFrom:DCF_SHOW_PART_CHECK_IP successHandler:^(DCFResponseModel *response) {
        if (response.success) {
            if ([response.responseData[@"iOS_banner"] isKindOfClass:[NSString class]] && [response.responseData[@"iOS_banner"] length] > 0) {
                _homeUrl = response.responseData[@"iOS_banner"];
                if ([response.responseData[@"ipCheckResult"] boolValue]) {
                    [self selectHomePage];
                }else{
                    [self goClothesHome];
                }
                return ;
            }
        }
        [self goClothesHome];
    } failHandler:^(NSError *error) {
        [self goClothesHome];
    }];
}

-(void)goToShellHome{
    //展示马甲
}

-(void)goClothesHome{
    //展示维护中
    DCFCheckingViewController *vc = [[DCFCheckingViewController alloc] init];
    [[[UIApplication sharedApplication] delegate] window].rootViewController = vc;
    [[[[UIApplication sharedApplication] delegate] window] makeKeyAndVisible];
}

-(void)selectHomePage{
    [[[UIApplication sharedApplication] delegate] window].backgroundColor = [UIColor whiteColor];
    if (![[DCFLoginManager sharedInstance] isLogin]) {
        WEAKSELF
        DCFInputPhoneViewController *inputVC = [[DCFInputPhoneViewController alloc] init];
        inputVC.completeBlock = ^{
            [weakSelf goToHomePage];
        };
        [[[UIApplication sharedApplication] delegate] window].rootViewController = inputVC;
        [[[[UIApplication sharedApplication] delegate] window] makeKeyAndVisible];
    }else{
        [self goToHomePage];
    }
}

-(void)goToHomePage{
    DCFHomeWebUrlViewController *vc = [[DCFHomeWebUrlViewController alloc] init];
    vc.url = _homeUrl;
    DCFNavigationController *nav = [[DCFNavigationController alloc] initWithRootViewController:vc];
    nav.navigationBar.barTintColor = RGBCOLOR(48, 130, 253);
    [[[UIApplication sharedApplication] delegate] window].rootViewController = nav;
    [[[[UIApplication sharedApplication] delegate] window] makeKeyAndVisible];
}


@end
